<?php
    class AdminDthink_Model_User extends Zend_Db_Table_Abstract
    {
        protected $_name = 'user';
        protected $_primary = 'user_id';
        protected $_db;
        
        public function __construct()
        {
            $this->_db = Zend_Registry::get('db');
        }
        
        public function listUser($user_id)
        {
            $sql = $this->_db->select()
            				 ->from($this->_name)
                             ->where('user_id <> ?', $user_id)
            				 ->order('user_id DESC');
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
        
        public function checkUser($email, $user_id = '')
        {
            if($user_id != '')
            {
                $sql = $this->_db->select()
                                ->from($this->_name)
                                ->where('email = ?', $email)
                                ->where('user_id = ?', $user_id);
                $result = $this->_db->fetchAll($sql);
                return count($result);
            }
            else
            {
                $sql = $this->_db->select()
                                ->from($this->_name)
                                ->where('email = ?', $email);
                $result = $this->_db->fetchAll($sql);
                return count($result);
            }
        }
        
        public function getUserById($user_id)
        {
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('user_id = ?', $user_id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function addUser($email, $password, $avatar, $fullname, $gender, $birthday, $level, $provider_id = '')
        {
            if($provider_id != '')
            {
                $data = array(
                    'provider_id' => $provider_id,
                    'email' => $email,
                    'password' => md5($password),
                    'avatar' => $avatar,
                    'fullname' => $fullname,
                    'gender' => $gender,
                    'birthday' => $birthday,
                    'level' => $level,
                    'status' => 1,
                );
            }
            else
            {
                $data = array(
                    'email' => $email,
                    'password' => md5($password),
                    'avatar' => $avatar,
                    'fullname' => $fullname,
                    'gender' => $gender,
                    'birthday' => $birthday,
                    'level' => $level,
                    'status' => 1,
                );
            }
            $result = $this->_db->insert($this->_name, $data);
            if($result)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        public function editUser($user_id, $email, $password = '', $avatar = '', $fullname, $gender, $birthday, $level, $status, $provider_id = '')
        {
            $where = 'user_id = ' . $user_id;
            if($avatar != '')
            {
                if($password != '')
                {
                    if($provider_id != '')
                    {
                        $data = array(
                            'provider_id' => $provider_id,
            				'email' => $email,
                            'password' => md5($password),
                            'avatar' => $avatar,
                            'fullname' => $fullname,
                            'gender' => $gender,
                            'birthday' => $birthday,
                            'level' => $level,
                            'status' => $status,
        			    );
                    }
                    else
                    {
                        $data = array(
            				'email' => $email,
                            'password' => md5($password),
                            'avatar' => $avatar,
                            'fullname' => $fullname,
                            'gender' => $gender,
                            'birthday' => $birthday,
                            'level' => $level,
                            'status' => $status,
        			    );
                    }
                }
                else
                {
                    if($provider_id != '')
                    {
                        $data = array(
                            'provider_id' => $provider_id,
            				'email' => $email,
                            'avatar' => $avatar,
                            'fullname' => $fullname,
                            'gender' => $gender,
                            'birthday' => $birthday,
                            'level' => $level,
                            'status' => $status,
            			);
                    }
                    else
                    {
                        $data = array(
            				'email' => $email,
                            'avatar' => $avatar,
                            'fullname' => $fullname,
                            'gender' => $gender,
                            'birthday' => $birthday,
                            'level' => $level,
                            'status' => $status,
            			);
                    }
                }
    			$result = $this->_db->update($this->_name, $data, $where);
    			if($result)
    			{
    				return true;
    			}
    			else
    			{
    				return false;
    			}
            }
            else
            {
                if($password != '')
                {
                    if($provider_id != '')
                    {
                        $data = array(
                            'provider_id' => $provider_id,
            				'email' => $email,
                            'password' => md5($password),
                            'fullname' => $fullname,
                            'gender' => $gender,
                            'birthday' => $birthday,
                            'level' => $level,
                            'status' => $status,
            			);
                    }
                    else
                    {
                        $data = array(
            				'email' => $email,
                            'password' => md5($password),
                            'fullname' => $fullname,
                            'gender' => $gender,
                            'birthday' => $birthday,
                            'level' => $level,
                            'status' => $status,
            			);
                    }
                }
                else
                {
                    if($provider_id != '')
                    {
                        $data = array(
                            'provider_id' => $provider_id,
            				'email' => $email,
                            'fullname' => $fullname,
                            'gender' => $gender,
                            'birthday' => $birthday,
                            'level' => $level,
                            'status' => $status,
            			);
                    }
                    else
                    {
                        $data = array(
            				'email' => $email,
                            'fullname' => $fullname,
                            'gender' => $gender,
                            'birthday' => $birthday,
                            'level' => $level,
                            'status' => $status,
            			);
                    }
                }
    			$result = $this->_db->update($this->_name, $data, $where);
    			if($result)
    			{
    				return true;
    			}
    			else
    			{
    				return false;
    			}
            }
        }
        
        public function deleteUser($user_id)
        {
            $where = 'user_id = ' . $user_id;
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where($where);
            $row = $this->_db->fetchRow($sql);
            $result = $this->_db->delete($this->_name, $where);
            if($result)
            {
                if (is_file(PUBLIC_PATH . '/files/user/' . $row['avatar']))
    			{
    				unlink(PUBLIC_PATH . '/files/user/' . $row['avatar']);
    			}
                return true;
            }
            else
            {
                return false;
            }
        }
        
        public function deleteUsers($listId)
        {
            foreach($listId as $value)
            {
                $where = 'user_id = ' . $value;
                $sql = $this->_db->select()
    							 ->from($this->_name)
    							 ->where($where);
    			$row = $this->_db->fetchRow($sql);
                if (is_file(PUBLIC_PATH . '/files/user/' . $row['avatar']))
    			{
    				unlink(PUBLIC_PATH . '/files/user/' . $row['avatar']);
    			}
    			$result = $this->_db->delete($this->_name, $where);
            }
        }
        
        public function searchUser($keyword)
        {
            $where = "MATCH(email, fullname) AGAINST('" . $keyword . "' IN NATURAL LANGUAGE MODE)";
			$sql = $this->_db->select()
							 ->from($this->_name)
							 ->where($where);
			$result = $this->_db->fetchAll($sql);
			return $result;
        }
        
        public function delImg($user_id)
        {
            $data = array(
                'avatar' => 0,
            );
            $where = 'user_id = ' . $user_id;
            $result = $this->_db->update($this->_name, $data, $where);
            if($result)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        public function checkPassword($user_id, $password)
        {
        	$where = $this->_primary . " = " . $user_id . " AND password = '" . md5($password) . "'";
        	$sql = $this->_db->select()
        					 ->from($this->_name)
        					 ->where($where);
        	$result = $this->_db->fetchAll($sql);
        	if(count($result) > 0)
        	{
        		return true;
        	}
        	else
        	{
        		return false;
        	}
        }
        
        public function changePasswordUser($user_id, $newPassword)
        {
        	$data = array(
        		'password' => md5($newPassword),
        	);
        	$where = $this->_primary . ' = ' . $user_id;
        	$result = $this->_db->update($this->_name, $data, $where);
        	if($result)
        	{
        		return true;
        	}
        	else
        	{
        		return false;
        	}
        }
    }
